package com.java.day07jdbc;

import java.sql.*;

public class DriverManagerDemo {

    public static final String URL = "jdbc:mysql://localhost:3306/testdb?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8&useSSL=false";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "123456";

    public static void main(String[] args){
        //获得连接对象,自动关闭连接
        try (Connection connection = DriverManager.getConnection(URL,USERNAME,PASSWORD)){
            //获得命令对象
            Statement statement = connection.createStatement();
            //执行SQL语句
//            int rs = statement.executeUpdate("insert into student(name,age,gender,address) values('张三风',30,'男','武当山')");
            //执行查询语句，获得结果集
            ResultSet resultSet = statement.executeQuery("select * from student");
            //循环读取每一行数据
            while(resultSet.next()){
                //读取每一行的每一列
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String address = resultSet.getString("address");
                System.out.println(id +"--" + name + "--" + age + "--" + address);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
